package com.controller;

import com.dto.BaseDto;
import com.entity.Admin;
import com.page.PageInfo;
import com.service.AdminService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Optional;

@Controller
public class AdminController {
    @Resource
    AdminService adminService;

//传统传参，不需要绑定参数 url直接在后面写上？adid=0001
    @RequestMapping("/admin/getByAdidJson")
    @ResponseBody
    public Admin getByAdidJson(String adid) {
        return adminService.getByAdid(adid);
    }

//rest风格传参 将参数变为url的一部分但是需要绑定 例如 /admin/getByAdidNew/0001
    @RequestMapping("/admin/getByAdidNewJSon/{adid}")
    @ResponseBody
    public Admin getByAdidNewJson(@PathVariable("adid") String adid) {
        return adminService.getByAdid(adid);
    }

    @RequestMapping("/admin/getAllJson")
    @ResponseBody
    public BaseDto<Admin> getAllJson() {
        return adminService.getAll();

    }

    @RequestMapping("/admin/getAll")
    public ModelAndView getAll() {
       BaseDto<Admin> dto = adminService.getAll();
        return new ModelAndView("/back/admin/list","dto",dto);

    }

    @RequestMapping("/admin/getAllByPage")
    public ModelAndView getAllByPage(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Admin> dto = adminService.getAllByPage(pageInfo);
        ModelAndView mv = new ModelAndView("/back/admin/list","dto",dto);
        return mv;
    }
    @RequestMapping("/admin/getAllByPageJson")
    @ResponseBody
    public BaseDto getAllByPageJson(PageInfo pageInfo) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Admin> dto = adminService.getAllByPage(pageInfo);
        return dto;
    }


    @RequestMapping("/admin/searchNameByPage/{adname}")
    @ResponseBody
    public BaseDto searchNameByPage(PageInfo pageInfo,@PathVariable("adname")String adname) {
        //页面传递的pageInfo可能为空或者没有值，进行判空处理
        Optional<PageInfo> pageInfoOptional = Optional.ofNullable(pageInfo);
        pageInfo = pageInfoOptional.isPresent()?pageInfo:new PageInfo();
        BaseDto<Admin> dto = adminService.searchNameByPage(pageInfo,adname);
        return dto;
    }


    /**
     * 根据主键查询
     * @param adid
     * @return
     */
    @RequestMapping("/admin/getAdminByAdid/{adid}")
    @ResponseBody
    public BaseDto getAdminByAdid(@PathVariable("adid") String adid){
        return adminService.getAdminByAdid(adid);
    }

    /**
     * 添加一个实体
     * @param admin
     * @return
     */
    @RequestMapping("/admin/addAdmin")
    @ResponseBody
    public BaseDto addAdmin(Admin admin){
        return adminService.addAdmin(admin);
    }
    /**
     * 修改实体
     */
    @RequestMapping("/admin/updateAdmin")
    @ResponseBody
    public BaseDto updateAdmin(Admin admin){
        return adminService.updateAdmin(admin);
    }
    public BaseDto updateAdminByExample(){
        return null;
    }

    /**
     * 删除一个实体
     * @param adid
     * @return
     */
    @RequestMapping("/admin/deleteAdminByPrimayKey/{adid}")
    @ResponseBody
    public BaseDto deleteAdminByPrimayKey(@PathVariable("adid") String adid){
        return adminService.deleteAdminByPrimayKey(adid);
    }

    /**
     * 删除实体根据示例
     * @return
     */
    public BaseDto deleteAdminByExample(){
        return null;
    }

}
